package net.thxjj.adminServer.mongodao;

import net.thxjj.adminServer.entity.LoginHadleEvent;
import net.thxjj.adminServer.entity.aep.ProfileLoginData;
import net.thxjj.adminServer.entity.onenet.OnetEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@Component
public class MongoOnenetDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    //导入所有数据
    public void saveAllData(List<OnetEvent> list){
        mongoTemplate.insert(list,OnetEvent.class);
    }


    //导入校验（如果有timestamp和deviceId重合则更新）
    public void saveOnetDatas(List<OnetEvent> list){

        for (OnetEvent profile:
                list) {
            OnetEvent po = findOneNetDataByDeviceIdAndTime(profile.getDeviceId(),profile.getCreateTime());
            if(po==null){
                mongoTemplate.save(profile);
            }else{
                updateOnetData(profile);
            }
        }
    }

    public void saveOnetData(OnetEvent onetEvent){
        OnetEvent po = findOneNetDataByDeviceIdAndTime(onetEvent.getDeviceId(),onetEvent.getCreateTime());
        if(po==null){
            mongoTemplate.save(onetEvent);
        }else{
            updateOnetData(onetEvent);
        }
    }

    private void updateOnetData(OnetEvent onetEvent) {

        Query query = new Query(Criteria.where("deviceId").is(onetEvent.getDeviceId()).and("createTime").is(onetEvent.getCreateTime()));
        Update update = new Update()
                .set("eventContent",onetEvent.getEventContent());

        mongoTemplate.upsert(query,update, OnetEvent.class);
    }

    private OnetEvent findOneNetDataByDeviceIdAndTime(String deviceId, long time){
        Query query = new Query(Criteria.where("deviceId").is(deviceId).and("createTime").is(time));
        return mongoTemplate.findOne(query,OnetEvent.class);
    }


    public OnetEvent getSmokeStatus(String device_id){
        String pattern1 = String.format("^.{%s}$", 62);
        String pattern2 = String.format("^.{%s}$", 124);
        Criteria criteria1 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern1));
        Criteria criteria2 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern2));
        Query query = new Query(new Criteria().orOperator(criteria1,criteria2));
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        return mongoTemplate.findOne(query,OnetEvent.class);
    }


    public List<OnetEvent> getSmokeStatusList(String device_id){
        String pattern1 = String.format("^.{%s}$", 62);
        String pattern2 = String.format("^.{%s}$", 124);
        Criteria criteria1 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern1));
        Criteria criteria2 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern2));
        Query query = new Query(new Criteria().orOperator(criteria1,criteria2));
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        return mongoTemplate.find(query,OnetEvent.class);
    }

    public List<OnetEvent> getSmokeStatusListByTime(String device_id,long start_time,long end_time){
        String pattern1 = String.format("^.{%s}$", 62);
        String pattern2 = String.format("^.{%s}$", 124);
        Criteria criteria1 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern1),Criteria.where("createTime").gt(start_time),Criteria.where("createTime").lt(end_time));
        Criteria criteria2 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern2),Criteria.where("createTime").gt(start_time),Criteria.where("createTime").lt(end_time));
        Query query = new Query(new Criteria().orOperator(criteria1,criteria2));
        query.with(new Sort(Sort.Direction.ASC,"createTime"));
        return mongoTemplate.find(query,OnetEvent.class);
    }

    public List<OnetEvent> getSmokeAllListByTime(String device_id,long start_time,long end_time){
        Criteria criteria1 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("createTime").gt(start_time),Criteria.where("createTime").lt(end_time));
        Query query = new Query(criteria1);
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        return mongoTemplate.find(query,OnetEvent.class);
    }

    public OnetEvent getNewestSmokeStatus(String device_id,long time){
        String pattern1 = String.format("^.{%s}$", 62);
        String pattern2 = String.format("^.{%s}$", 124);
        Criteria criteria1 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern1),Criteria.where("createTime").lt(time));
        Criteria criteria2 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern2),Criteria.where("createTime").lt(time));
        Query query = new Query(new Criteria().orOperator(criteria1,criteria2));
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        return mongoTemplate.findOne(query,OnetEvent.class);
    }

    public OnetEvent getSmokeLoginEvent(String device_id){
        String pattern1 = String.format("^.{%s,}$", 132);
        Criteria criteria1 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern1));
        Query query = new Query(criteria1);
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        return mongoTemplate.findOne(query,OnetEvent.class);
    }

    public List<OnetEvent> getSmokeLoginEventList(String device_id,long start_time){
        String pattern1 = String.format("^.{%s,}$", 132);
        Criteria criteria1 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern1),Criteria.where("createTime").gte(start_time));
        Query query = new Query(criteria1);
        query.with(new Sort(Sort.Direction.ASC,"createTime"));
        return mongoTemplate.find(query,OnetEvent.class);
    }

    private List<OnetEvent> getSmokeLoginList(String device_id){
        String pattern1 = String.format("^.{%s,}$", 132);
        Criteria criteria1 = Criteria.where("deviceId").is(device_id).andOperator(Criteria.where("eventContent").regex(pattern1));
        Query query = new Query(criteria1);
        query.with(new Sort(Sort.Direction.ASC,"createTime"));
        return mongoTemplate.find(query,OnetEvent.class);
    }

    public List<LoginHadleEvent> getLoginHandleWithLog(String device_id){
        List<OnetEvent> list=  getSmokeLoginList(device_id);
        Query query = new Query(Criteria.where("deviceId").is(device_id));
        query.with(new Sort(Sort.Direction.ASC,"timestamp"));
        List<LoginHadleEvent> list2 = mongoTemplate.find(query,LoginHadleEvent.class);
        for (OnetEvent logindata:
                list) {
            LoginHadleEvent loginHadleEvent = new LoginHadleEvent().setDeviceId(logindata.getDeviceId()).setTimestamp(logindata.getCreateTime()).setUserId(0);
            list2.add(loginHadleEvent);
        }
        Collections.sort(list2);
        return list2;
    }

}
